<?php

namespace backend\widgets;

use Yii;
use yii\base\Widget;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;

/** <?= TemplateDetailsForm::widget(['model' => $model, 'gameConfs' => $gameConfs]) ?> */
class TemplateDetailsForm extends Widget
{
    public $model;
    public $gameConfs;

    public function init()
    {
        parent::init();

        if (empty($this->model)) {
            throw new InvalidConfigException('Required `model` param isn\'t set.');
        }
    }

    public function run()
    {
        $this->model->gameConfDetails;

        $gameConfDtails = $gameConfs = [];
        if (!empty($this->model->game_conf_details)) {

            foreach ($this->model->game_conf_details as $key => $value) {
                $gameConfDtails[$value['meta_key']]['model_meta_key'] = $value['meta_key'];
                $gameConfDtails[$value['meta_key']]['model_meta_name'] = $value['meta_name'];
                $gameConfDtails[$value['meta_key']]['model_meta_value'] = !empty($value['meta_value']) ? $value['meta_value'] : '';
                $gameConfDtails[$value['meta_key']]['model_type'] = $value['type'];
                $gameConfDtails[$value['meta_key']]['model_is_write'] = $value['is_write'];
                $gameConfDtails[$value['meta_key']]['model_is_show'] = $value['is_show'];
            }
        }

        $this->gameConfs = ArrayHelper::toArray($this->gameConfs);
        if (!empty($this->gameConfs)) {
            foreach ($this->gameConfs as $key => $value) {
                $gameConfs[$value['meta_key']]['meta_key'] = $value['meta_key'];
                $gameConfs[$value['meta_key']]['meta_name'] = $value['meta_name'];
                $gameConfs[$value['meta_key']]['meta_value'] = $value['meta_value'];
                $gameConfs[$value['meta_key']]['type'] = $value['type'];
                $gameConfs[$value['meta_key']]['model_meta_value'] = !empty($gameConfDtails[$value['meta_key']]['model_meta_value']) ? $gameConfDtails[$value['meta_key']]['model_meta_value'] : '';
                $gameConfs[$value['meta_key']]['model_is_write'] = !empty($gameConfDtails[$value['meta_key']]['model_is_write']) ? $gameConfDtails[$value['meta_key']]['model_is_write'] : 0;
                if ($this->model->isNewRecord) {
                    $gameConfs[$value['meta_key']]['model_is_show'] = 1;
                } else {
                    $gameConfs[$value['meta_key']]['model_is_show'] = !empty($gameConfDtails[$value['meta_key']]['model_is_show']) ? $gameConfDtails[$value['meta_key']]['model_is_show'] : 0;
                }
            }
        }

        if (!empty($gameConfs)) {
            foreach ($gameConfs as &$key) {
                $key['format_meta_value'] = !empty($key['meta_value']) ? explode(',', $key['meta_value']) : '';
                if (!empty($key['format_meta_value'])) {
                    $key['format_meta_value'] = $this->valueToKey($key['format_meta_value']);
                }

                $key['format_model_meta_value'] = !empty($key['model_meta_value']) ? explode(',', $key['model_meta_value']) : '';
                if (!empty($key['format_model_meta_value'])) {
                    $key['format_model_meta_value'] = $this->valueToKey($key['format_model_meta_value']);
                }
                switch ($key['type']) {
                    case 'text':
                    case 'image':
                    case 'file':
                        $key['format_meta_value'] = !empty($key['meta_value']) ? $key['meta_value'] : '';
                        $key['format_model_meta_value'] = !empty($key['model_meta_value']) ? $key['model_meta_value'] : '';
                        break;
                    default:
                        break;
                }
            }
            unset($key);
        }

        $details = array_values($gameConfs);

        return $this->render('template-details-form', [
            'model' => $this->model,
            'details' => $details,
        ]);
    }

    public function valueToKey($param = [])
    {
        $res = [];
        if (!empty($param) && is_array($param)) {
            foreach ($param as $key => $value) {
                $res[$value] = $value;
            }
        }
        return $res;
    }

}